<template>
  <view>
    <!-- 整个商品列表大盒子 -->
    <view class="goodsList-box">
      <view  v-for="(item,index) in goodsList" :key="index" @click="gotoDetail(item)"> 
        <my-goods :item="item" ></my-goods>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        queryObj:{
          query:'',
          cid:'',
          pagenum:1,
          pagesize:10
        },
        goodsList:[],
        total:0,
        isLoading:false
      
        
      }
    },
    onLoad(options) {
      console.log(options)
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.goods_id || ''
      this.getGoodsLists()
    },
    methods: {
     async getGoodsLists(pd){
       this.isLoading = true
        const res = await uni.$http.get('/api/public/v1/goods/search',this.queryObj)
        //短路，如果pd传过来了，就执行pd，即停止下拉刷新
        pd && pd()
        if(res.data.meta.status!==200) return uni.$showMsg
        //下拉刷新时，要衔接上之前请求回来的数据
        this.goodsList = [...this.goodsList,...res.data.message.goods]
        this.total = res.data.message.total
        console.log(res)
        this.isLoading = false
      },
      gotoDetail(item){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      }
    },
    onReachBottom() {
      //刷新时判断是否数据加载完毕
      if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕..')
      //节流,刷新时先判断是否正在请求数据
      if(this.isLoading) return
      //下拉刷新时，先给pagenum自加1，再发起加载列表请求
      this.queryObj.pagenum += 1
      this.getGoodsLists()
    },
    onPullDownRefresh() {
      //下拉刷新
      //重置数据,渲染第一页数据
      this.queryObj.pagenum = 1
      this.total = 0
      //商品列表置空
      this.goodsList = []
      //关闭节流阀
      this.isLoading = false
      //传一个回调函数用于禁止下拉刷新
      this.getGoodsLists(()=>{
        uni.stopPullDownRefresh()
      })
      
    }
  }
</script>

<style lang="scss">

</style>
